﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
using Infragistics.Win.UltraWinCalcManager;
using Infragistics.Win.CalcEngine;
using System.Collections;
using System.Text.RegularExpressions;

namespace BYManageInfoSystem
{
    public partial class Frm_FeeDZTruePayTab : DockContent
    {
        private DGBY.BLL.DG_FeeLockTab m_bllDG_FeeLockTab = new DGBY.BLL.DG_FeeLockTab();
        private DGBY.BLL.DG_FeeTruePayLockTab m_bllDG_FeeTruePayLockTab = new DGBY.BLL.DG_FeeTruePayLockTab();
        private string m_strBianHaoTemp = "";
        private bool m_bClearing = false;
        private DGBY.BLL.DZRecvPeoTelTab m_bllDZRecvPeoTelTab = new DGBY.BLL.DZRecvPeoTelTab();
        private int m_nFacID = 0;
        private DGBY.BLL.FeeDZTruePayTab m_bllFeeDZTruePayTab = new DGBY.BLL.FeeDZTruePayTab();
        private bool m_bInsertFlag = false;
        private int m_nDelIndex = -1;
        private DGBY.Model.FeeDZTruePayTab m_modelFeeDZTruePayTab = new DGBY.Model.FeeDZTruePayTab();
        private DGBY.BLL.HxdNoTab m_bllHxdNoTab = new DGBY.BLL.HxdNoTab();
        Hashtable m_SQLStringList = new Hashtable();
        ArrayList m_arrayList = new ArrayList();
        private DGBY.BLL.SJPackingTab m_bllSJPackingTab = new DGBY.BLL.SJPackingTab();
        private DGBY.BLL.DZCompDataTab m_bllDZCompDataTab = new DGBY.BLL.DZCompDataTab();
        private DGBY.BLL.DZCateTab m_bllDZCateTab = new DGBY.BLL.DZCateTab();
        private DGBY.BLL.FactoryDataTab m_bllFactoryDataTab = new DGBY.BLL.FactoryDataTab();

        private DGBY.BLL.T_ACL_User m_bllT_ACL_User = new DGBY.BLL.T_ACL_User();
        private DGBY.BLL.PaidanRegisterTab m_bllPaidanRegisterTab = new DGBY.BLL.PaidanRegisterTab();
        private InsertType m_InsertType = new InsertType();
        public Frm_FeeDZTruePayTab()
        {
            InitializeComponent();
        }

        private void InitComBox()
        {
            this.comboBoxShowWay.Items.AddRange(new object[] { "最近半年记录", "全部记录" });
            this.comboBoxShowWay.SelectedIndex = 0;

            DataSet ds = m_bllDZCompDataTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxComp.DataSource = ds.Tables[0];

                this.comboBoxComp.ValueMember = "DZCompDataTabID";
                this.comboBoxComp.DisplayMember = "公司简称";
                this.comboBoxComp.AutoCompleteSource = AutoCompleteSource.ListItems;
                this.comboBoxComp.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                this.comboBoxComp.SelectedIndex = 1;
            }
        }

        private void ShowAllRecordInDataGridView()
        {
            string strTemp = this.comboBoxShowWay.Text;
            if (strTemp == "全部记录")
                ShowAllRecord("");
            else
            {

                DateTime dtNow = System.DateTime.Now.Date.AddDays(-180);
                ShowAllRecord("case when year(HxdNoTab.SBDate) = 1900 then getdate() else HxdNoTab.SBDate end >= '" + dtNow + "' or FeeDZTruePayTab.PaidanRegisterTabid = -1");
            }
        }

        private void ShowAllRecord(string strWhere)
        {
            DataSet ds = m_bllFeeDZTruePayTab.GetList(strWhere);
            ultraGrid1.DataSource = null;



            //DataSet dsTemp = m_bllDZCompDataTab.GetList("");

            //this.ultraDropDownFac.DataSource = dsTemp.Tables[0];
            //this.ultraDropDownFac.DisplayMember = "单证公司名称";
            //this.ultraDropDownFac.ValueMember = "DZCompDataTabID";
            //this.ultraDropDownFac.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            //for (int i = 0; i < this.ultraDropDownFac.DisplayLayout.Bands[0].Columns.Count; i++)
            //    this.ultraDropDownFac.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            //this.ultraDropDownFac.DisplayLayout.Bands[0].Columns["单证公司名称"].Hidden = false;

            DataSet dsTemp = m_bllDZCateTab.GetList("");

            this.ultraDropDownGuiXing.DataSource = dsTemp.Tables[0];
            this.ultraDropDownGuiXing.DisplayMember = "单证种类";
            this.ultraDropDownGuiXing.ValueMember = "DZCateTabID";
            this.ultraDropDownGuiXing.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            for (int i = 0; i < this.ultraDropDownGuiXing.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownGuiXing.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownGuiXing.DisplayLayout.Bands[0].Columns["单证种类"].Hidden = false;

            dsTemp = m_bllFactoryDataTab.GetList("");

            this.ultraDropDownFac.DataSource = dsTemp.Tables[0];
            this.ultraDropDownFac.DisplayMember = "工厂简称";
            this.ultraDropDownFac.ValueMember = "FactoryDataTabID";
            this.ultraDropDownFac.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            for (int i = 0; i < this.ultraDropDownFac.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownFac.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownFac.DisplayLayout.Bands[0].Columns["工厂简称"].Hidden = false;

            dsTemp = m_bllT_ACL_User.GetList("");

            this.ultraDropDownZhiDanPerson.DataSource = dsTemp.Tables[0];
            this.ultraDropDownZhiDanPerson.DisplayMember = "用户名称";
            this.ultraDropDownZhiDanPerson.ValueMember = "UserPasswordTabID";
            this.ultraDropDownZhiDanPerson.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            for (int i = 0; i < this.ultraDropDownZhiDanPerson.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownZhiDanPerson.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownZhiDanPerson.DisplayLayout.Bands[0].Columns["用户名称"].Hidden = false;

            ultraGrid1.DataSource = ds.Tables[0];


            ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            ultraGrid1.DisplayLayout.Bands[0].Columns[1].Hidden = true;
            ultraGrid1.DisplayLayout.Bands[0].Columns[2].Hidden = true;
            //ultraGrid1.DisplayLayout.Bands[0].Columns["实付费用计提年月"].Hidden = true;
            ultraGrid1.DisplayLayout.Bands[0].Columns["应收金额合计公式"].Hidden = true;
            ultraGrid1.DisplayLayout.Bands[0].Columns["应付实付金额合计公式"].Hidden = true;
            //ultraGrid1.DisplayLayout.Bands[0].Columns[3].Hidden = true;

           // this.ultraGrid1.DisplayLayout.Bands[0].Columns["单证公司"].ValueList = this.ultraDropDownFac;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["单证种类"].ValueList = this.ultraDropDownGuiXing;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["工厂简称"].ValueList = this.ultraDropDownFac;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["制单人员"].ValueList = this.ultraDropDownZhiDanPerson;

            //ultraComboEditor1.DataSource = ds.Tables[0];
            //ultraComboEditor1.DisplayMember = "收款人";
            //ultraComboEditor1.ValueMember = "收款人";


        }
        private void SyncSBMonth()
        {
            DataSet dsTemp = m_bllHxdNoTab.ShowZhDjbRec("year(HxdNoTab.SBDate) > 1900");
            DataSet ds = m_bllFeeDZTruePayTab.GetList("ShouldPayMonth = 0 or TruePayMonth = 0");
            for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
            {
                string strContractNo = ds.Tables[0].Rows[j]["合同编号"].ToString();
                int nFeeDZTruePayTabID = (int)ds.Tables[0].Rows[j]["FeeDZTruePayTabID"];
                DGBY.Model.FeeDZTruePayTab modelFeeDZTruePayTabOld
               = m_bllFeeDZTruePayTab.GetModel(nFeeDZTruePayTabID);

                DataRow[] Rows = dsTemp.Tables[0].Select("合同编号 = '" + strContractNo.ToString() + "'");
                if (Rows.Length > 0)
                {
                    DateTime dtSBMonth = (DateTime)Rows[0]["申报日期"];
                    int nSBMonth = dtSBMonth.Month;
                    modelFeeDZTruePayTabOld.ShouldPayMonth = nSBMonth;
                    modelFeeDZTruePayTabOld.TruePayMonth = nSBMonth;
                    m_bllFeeDZTruePayTab.Update(modelFeeDZTruePayTabOld);
                }

            }
         
        }

        private void Frm_FeeDZTruePayTab_Load(object sender, EventArgs e)
        {
            //SyncSBMonth();
            ClassUltraGridCommonSet.InitUltraWinGridSetting(this.ultraGrid1);
        }

        private void buttonShowAll_Click(object sender, EventArgs e)
        {
            ShowAllRecordInDataGridView();
        }

        public static void ultraGrid1_InitializeRowFilter(Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            //ultraGrid1.DisplayLayout.Override.RowSizing = RowSizing.AutoFree;
            //foreach (UltraGridColumn ugc in ultraGrid1.DisplayLayout.Bands[0].Columns)
            //    ugc.PerformAutoResize();
            // FILTER ROW FUNCTIONALITY RELATED ULTRAGRID SETTINGS
            //e.Layout.Override.RowFilterAction = RowFilterAction.DisableFilteredOutRows;
            // ----------------------------------------------------------------------------------
            // Enable the the filter row user interface by setting the FilterUIType to FilterRow.
            e.Layout.Override.FilterUIType = FilterUIType.FilterRow;
            //e.Layout.Override.RowFilterMode = RowFilterMode.AllRowsInBand;
            // FilterEvaluationTrigger specifies when UltraGrid applies the filter criteria typed 
            // into a filter row. Default is OnCellValueChange which will cause the UltraGrid to
            // re-filter the data as soon as the user modifies the value of a filter cell.
            e.Layout.Override.FilterEvaluationTrigger = FilterEvaluationTrigger.OnCellValueChange;
            //e.Layout.Override.RowFilterMode = RowFilterMode.AllRowsInBand;
            // By default the UltraGrid selects the type of the filter operand editor based on
            // the column's DataType. For DateTime and boolean columns it uses the column's editors.
            // For other column types it uses the Combo. You can explicitly specify the operand
            // editor style by setting the FilterOperandStyle on the override or the individual
            // columns.

            e.Layout.Override.FilterOperandStyle = FilterOperandStyle.DropDownList;

            // By default UltraGrid displays user interface for selecting the filter operator. 
            // You can set the FilterOperatorLocation to hide this user interface. This
            // property is available on column as well so it can be controlled on a per column
            // basis. Default is WithOperand. This property is exposed off the column as well.
            e.Layout.Override.FilterOperatorLocation = FilterOperatorLocation.WithOperand;

            // By default the UltraGrid uses StartsWith as the filter operator. You use
            // the FilterOperatorDefaultValue property to specify a different filter operator
            // to use. This is the default or the initial filter operator value of the cells
            // in filter row. If filter operator user interface is enabled (FilterOperatorLocation
            // is not set to None) then that ui will be initialized to the value of this
            // property. The user can then change the operator as he/she chooses via the operator
            // drop down.
            e.Layout.Override.FilterOperatorDefaultValue = FilterOperatorDefaultValue.Contains;

            // FilterOperatorDropDownItems property can be used to control the options provided
            // to the user for selecting the filter operator. By default UltraGrid bases 
            // what operator options to provide on the column's data type. This property is
            // avaibale on the column as well.
            //e.Layout.Override.FilterOperatorDropDownItems = FilterOperatorDropDownItems.All;

            // By default UltraGrid displays a clear button in each cell of the filter row
            // as well as in the row selector of the filter row. When the user clicks this
            // button the associated filter criteria is cleared. You can use the 
            // FilterClearButtonLocation property to control if and where the filter clear
            // buttons are displayed.
            e.Layout.Override.FilterClearButtonLocation = FilterClearButtonLocation.RowAndCell;

            // Appearance of the filter row can be controlled using the FilterRowAppearance proeprty.
            e.Layout.Override.FilterRowAppearance.BackColor = Color.LightYellow;

            // You can use the FilterRowPrompt to display a prompt in the filter row. By default
            // UltraGrid does not display any prompt in the filter row.
            e.Layout.Override.FilterRowPrompt = "Click here to filter data...";

            // You can use the FilterRowPromptAppearance to change the appearance of the prompt.
            // By default the prompt is transparent and uses the same fore color as the filter row.
            // You can make it non-transparent by setting the appearance' BackColorAlpha property 
            // or by setting the BackColor to a desired value.
            e.Layout.Override.FilterRowPromptAppearance.BackColorAlpha = Alpha.Opaque;

            // By default the prompt is spread across multiple cells if it's bigger than the
            // first cell. You can confine the prompt to a particular cell by setting the
            // SpecialRowPromptField property off the band to the key of a column.
            //e.Layout.Bands[0].SpecialRowPromptField = e.Layout.Bands[0].Columns[0].Key;

            // Display a separator between the filter row other rows. SpecialRowSeparator property 
            // can be used to display separators between various 'special' rows, including for the
            // filter row. This property is a flagged enum property so it can take multiple values.
            e.Layout.Override.SpecialRowSeparator = SpecialRowSeparator.FilterRow;

            // You can control the appearance of the separator using the SpecialRowSeparatorAppearance
            // property.
            e.Layout.Override.SpecialRowSeparatorAppearance.BackColor = Color.FromArgb(233, 242, 199);


        }
        private void InitColumnWidth()
        {
            ultraGrid1.DisplayLayout.Override.WrapHeaderText = Infragistics.Win.DefaultableBoolean.True;

            ultraGrid1.DisplayLayout.Bands[0].Columns["跟单人员"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["经营单位"].Width = 40;
            ultraGrid1.DisplayLayout.Bands[0].Columns["部门"].Width = 40;
            ultraGrid1.DisplayLayout.Bands[0].Columns["工厂简称"].Width = 40;
            ultraGrid1.DisplayLayout.Bands[0].Columns["合同编号"].Width = 75;
            ultraGrid1.DisplayLayout.Bands[0].Columns["制单人员"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["单证公司"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["单证种类"].Width = 40;
            ultraGrid1.DisplayLayout.Bands[0].Columns["单证份数"].Width = 40;
            ultraGrid1.DisplayLayout.Bands[0].Columns["加页份数"].Width = 40;
            ultraGrid1.DisplayLayout.Bands[0].Columns["单证项目金额"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["加页金额"].Width = 40;
            ultraGrid1.DisplayLayout.Bands[0].Columns["其他费用金额"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["应付实付金额合计"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["应收金额合计"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["应收费用计提年月"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["实付日期"].Width = 70;
            ultraGrid1.DisplayLayout.Bands[0].Columns["对公费用编号"].Width = 130;
            ultraGrid1.DisplayLayout.Bands[0].Columns["对私费用编号"].Width = 130;
            ultraGrid1.DisplayLayout.Bands[0].Columns["申报日期"].Width = 70;
            ultraGrid1.DisplayLayout.Bands[0].Columns["申报年月"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["实付费用计提年月"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["编号是否手动生成"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["是否直接开票"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["录入日期"].Width = 95;
            ultraGrid1.DisplayLayout.Bands[0].Columns["经办人"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["收款人"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["备注"].Width = 100;
  
            ultraGrid1.DisplayLayout.Bands[0].Columns["应收金额合计公式"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["应付实付金额合计公式"].Width = 60;
        }
        private void ultraGrid1_InitializeLayout(object sender, InitializeLayoutEventArgs e)
        {
            InitColumnWidth();
            SummarySettings averageSummary =
e.Layout.Bands[0].Summaries.Add(
"应付实付金额合计",
SummaryType.Sum,
e.Layout.Bands[0].Columns["应付实付金额合计"]);
            averageSummary.SummaryPosition = SummaryPosition.UseSummaryPositionColumn;
            averageSummary.SummaryPositionColumn = averageSummary.SourceColumn;
            averageSummary.DisplayFormat = "{0:.##}";
            averageSummary.Band.Override.SummaryFooterCaptionVisible = DefaultableBoolean.False;
            averageSummary.Band.Override.BorderStyleSummaryFooter = UIElementBorderStyle.None;
            averageSummary.SummaryDisplayArea = SummaryDisplayAreas.TopFixed;

            averageSummary =
e.Layout.Bands[0].Summaries.Add(
"应收金额合计",
SummaryType.Sum,
e.Layout.Bands[0].Columns["应收金额合计"]);
            averageSummary.SummaryPosition = SummaryPosition.UseSummaryPositionColumn;
            averageSummary.SummaryPositionColumn = averageSummary.SourceColumn;
            averageSummary.DisplayFormat = "{0:.##}";
            averageSummary.Band.Override.SummaryFooterCaptionVisible = DefaultableBoolean.False;
            averageSummary.Band.Override.BorderStyleSummaryFooter = UIElementBorderStyle.None;
            averageSummary.SummaryDisplayArea = SummaryDisplayAreas.TopFixed;


          //  e.Layout.Bands[0].Columns["收款人"].EditorControl = this.ultraComboEditor1;


            if (this.checkBox1.Checked == true)
                ultraGrid1_InitializeRowFilter(e);
            else
                ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);

            foreach (UltraGridColumn column2 in ultraGrid1.DisplayLayout.Bands[0].Columns)
            {
                string str = column2.Header.Caption;
                if (str == "合同编号" || str == "经营单位"
                    || str == "部门" || str == "应付实付金额合计公式" || str == "应收金额"
                    || str == "对公费用编号" || str == "对私费用编号" || str == "跟单人员"
                    || str == "录入日期" || str == "商检日期" || str == "经办人"
                    || str == "单证项目金额" || str == "加页金额"
                    || str == "集装箱柜号" || str == "核销单号" || str == "实付日期")
                {
                    column2.CellAppearance.BackColor = Color.Coral;
                    column2.CellActivation = Activation.ActivateOnly;
                }
            }
        }

        private void ultraGrid1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && e.Modifiers == Keys.Control)
            {

                if (!m_bInsertFlag)
                {
                    UltraGridRow CurrentUGRFrom;
                    CurrentUGRFrom = ultraGrid1.Rows[m_nDelIndex];
                    int nSelectFeeDZTruePayTabID = Convert.ToInt32(CurrentUGRFrom.Cells[0].Value.ToString(), 10);
                    m_modelFeeDZTruePayTab = m_bllFeeDZTruePayTab.GetModel(nSelectFeeDZTruePayTabID);
                    m_modelFeeDZTruePayTab.InputDate = System.DateTime.Now.Date;
                    // m_bCopyInsertFlag = true;
                    m_InsertType = InsertType.Common;
                    UltraGridRow row = this.ultraGrid1.DisplayLayout.Bands[0].AddNew();
                    //UltraGridRow row = this.ultraGrid1.DisplayLayout.Bands[0].AddNew();
                    //  row.Cells[2] = DateTime.Parse("1900-01-01");
                }
            }
            switch (e.KeyCode)
            {

                case Keys.Up:
                    {
                        UltraGridCell gridCell = this.ultraGrid1.ActiveCell;
                        if (null == gridCell)
                            return;     

                        string strTemp = this.ultraGrid1.ActiveCell.Column.Header.Caption;
                        if (strTemp == "单证种类")
                            return;
                        int nColumnIndex = this.ultraGrid1.ActiveCell.Column.Index;
                        int nIndex = ultraGrid1.ActiveRow.Index;
                        if (nIndex > 0)
                            nIndex--;
                        this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid1.PerformAction(UltraGridAction.AboveCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Down:
                    {
                        UltraGridCell gridCell = this.ultraGrid1.ActiveCell;
                        if (null == gridCell)
                            return;     

                        string strTemp = this.ultraGrid1.ActiveCell.Column.Header.Caption;
                        if (strTemp == "单证种类")
                            return;
                        int nColumnIndex = this.ultraGrid1.ActiveCell.Column.Index;
                        int nIndex = ultraGrid1.ActiveRow.Index;
                        if (nIndex < ultraGrid1.Rows.Count - 1)
                            nIndex++;
                        this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid1.PerformAction(UltraGridAction.BelowCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Right:
                    this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid1.PerformAction(UltraGridAction.NextCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
                case Keys.Left:
                    this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid1.PerformAction(UltraGridAction.PrevCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
            }
        }
        private void ResetRecord(int nFeeSJTruePayTabID)
        {

            try
            {
                DGBY.Model.FeeDZTruePayTab modelFeeDZTruePayTab
                    = m_bllFeeDZTruePayTab.GetModel(nFeeSJTruePayTabID);


                modelFeeDZTruePayTab.ToPubBianHao = "";
                modelFeeDZTruePayTab.ToPrivateBianHao = "";
                modelFeeDZTruePayTab.SBDate = DateTime.Parse("1900-01-01");
                modelFeeDZTruePayTab.TruePayDate = null;
                modelFeeDZTruePayTab.InputDate = null; 
                modelFeeDZTruePayTab.TruePayMonth = 0;
                modelFeeDZTruePayTab.ShouldPayMonth = 0;
                //modelFeeDZTruePayTab.TruePayMoney = 0;
                modelFeeDZTruePayTab.DZCounts = 0;
                modelFeeDZTruePayTab.AddPageCounts = 0;



                //modelFeeDZTruePayTab.ToPubFormula = "";
                //modelFeeDZTruePayTab.ToPrivateFormula = "";

                modelFeeDZTruePayTab.JinBanPerson = "";
                modelFeeDZTruePayTab.RecvMonyePerson = "";

                modelFeeDZTruePayTab.IsToRecvPayTab = 0;
                modelFeeDZTruePayTab.IsToFinCheckMoneyTab = 0;
                modelFeeDZTruePayTab.Note = "";
                // modelFeeBGTruePayTab.SjDate = System.DateTime.Now.Date;
                modelFeeDZTruePayTab.bitFiledReserv1 = false;
                modelFeeDZTruePayTab.bitFiledReserv2 = false;

                modelFeeDZTruePayTab.stringFiledReserv1 = "";
                modelFeeDZTruePayTab.stringFiledReserv2 = "";
                modelFeeDZTruePayTab.stringFiledReserv3 = "";
                modelFeeDZTruePayTab.stringFiledReserv4 = "";

                //modelFeeDZTruePayTab.intFiledReserv1 = (int)ds.Tables[0].Rows[0]["UserPasswordTabID"]; //制单人员
                modelFeeDZTruePayTab.intFiledReserv2 = 0;
                modelFeeDZTruePayTab.intFiledReserv3 = 0;
                modelFeeDZTruePayTab.intFiledReserv4 = 0;
                modelFeeDZTruePayTab.datetimeFiledReserv1 = System.DateTime.Parse("1900-01-01");
                modelFeeDZTruePayTab.datetimeFiledReserv2 = System.DateTime.Now.Date;

                modelFeeDZTruePayTab.decimalFiledReserv1 = 0;
                modelFeeDZTruePayTab.decimalFiledReserv2 = 0;
                modelFeeDZTruePayTab.decimalFiledReserv3 = 0;
                modelFeeDZTruePayTab.decimalFiledReserv4 = 0;
                m_bllFeeDZTruePayTab.Update(modelFeeDZTruePayTab);

                DGBY.Model.PaidanRegisterTab modelPaidanRegisterTab = m_bllPaidanRegisterTab.GetModel(modelFeeDZTruePayTab.PaidanRegisterTabID);
                if (null == modelPaidanRegisterTab)
                    Frm_Main.Log2DB(this.Text, "清空", modelFeeDZTruePayTab.FeeDZTruePayTabID.ToString());
                else
                    Frm_Main.Log2DB(this.Text, "清空", ultraGrid1.ActiveRow.Cells["合同编号"].Value.ToString());


            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }


        private void ResetUltraGridRow(int nRowIndex)
        {

            try
            {

                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nRowIndex];

                CurrentUGR.Cells["对公费用编号"].Value = "";
                CurrentUGR.Cells["对私费用编号"].Value = "";

                CurrentUGR.Cells["实付日期"].Value = DBNull.Value;

                CurrentUGR.Cells["录入日期"].Value = DBNull.Value;
                //CurrentUGR.Cells["录入日期"].Value = DBNull.Value;

                CurrentUGR.Cells["实付费用计提年月"].Value = 0;
                CurrentUGR.Cells["应收费用计提年月"].Value = 0;
                CurrentUGR.Cells["加页份数"].Value = 0;
                CurrentUGR.Cells["单证份数"].Value = 0;
                CurrentUGR.Cells["其他费用金额"].Value = 0;
                CurrentUGR.Cells["应付实付金额合计"].Value = 0;
                CurrentUGR.Cells["单证项目金额"].Value = 0;
                CurrentUGR.Cells["加页金额"].Value = 0;
                CurrentUGR.Cells["应收金额合计"].Value = 0;

                CurrentUGR.Cells["经办人"].Value = "";
                CurrentUGR.Cells["收款人"].Value = "";
              
                //CurrentUGR.Cells["是否已提取至应收应付登记表"].Value = m_modelFeeSJTruePayTab.IsToRecvPayTab;
                //CurrentUGR.Cells["是否已提取至财务对帐"].Value = m_modelFeeSJTruePayTab.IsToFinCheckMoneyTab;

                CurrentUGR.Cells["是否直接开票"].Value = false;
                CurrentUGR.Cells["备注"].Value = "";
                CurrentUGR.Cells["送单日期"].Value = DBNull.Value;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }
        private bool ModifyGoodsDetail(int nRowIndex)
        {

            try
            {
                if (m_bClearing)
                {
                    m_bClearing = false;
                    return false;
                }
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nRowIndex];
                bool bRet = CheckModify(CurrentUGR);
                if (!bRet)
                {
                    MessageBox.Show("费用已锁定，不能修改或删除记录！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
                int nFeeDZTruePayTabID = Convert.ToInt32(CurrentUGR.Cells[0].Value.ToString(), 10);
                DGBY.Model.FeeDZTruePayTab modelFeeDZTruePayTab
                    = m_bllFeeDZTruePayTab.GetModel(nFeeDZTruePayTabID);

                //20121205获取 数据
                int nDZCateTabIDOld = modelFeeDZTruePayTab.DZCateTabID;
                int nDZCountsOld = modelFeeDZTruePayTab.DZCounts;
                int nAddPageCounts = modelFeeDZTruePayTab.AddPageCounts;

                int nDZCompDataTabID = (int)this.comboBoxComp.SelectedValue;
                if (modelFeeDZTruePayTab.DZCompDataTabID != 1 && nDZCompDataTabID != modelFeeDZTruePayTab.DZCompDataTabID)
                {
                    if (MessageBox.Show("确实要变更单证公司么？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) != DialogResult.OK)
                    {
                        return false;
                    }
                }
                 


                int nPaidanRegisterTabID = (int)CurrentUGR.Cells["PaidanRegisterTabID"].Value;
                int nFactoryDataTabID = (int)CurrentUGR.Cells["FactoryDataTabID"].Value;

                if (nFactoryDataTabID > 0 && nPaidanRegisterTabID == -1)
                {
                    DataSet ds;
                    string strFac = CurrentUGR.Cells["工厂简称"].Value.ToString();
                    if (ClassInputValid.IsNum(strFac))
                    {
                        modelFeeDZTruePayTab.FactoryDataTabID = Convert.ToInt32(CurrentUGR.Cells["工厂简称"].Value.ToString(), 10);
                    }
                    else
                    {
                        ds = m_bllFactoryDataTab.GetList("FactoryNameJX = '" + strFac + "'");
                        if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                        {
                            modelFeeDZTruePayTab.FactoryDataTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["FactoryDataTabID"].ToString(), 10);
                        }
                    }


                }
                int intFiledReserv1 = modelFeeDZTruePayTab.intFiledReserv1;
                string strBGH = CurrentUGR.Cells["制单人员"].Value.ToString();
                if (ClassInputValid.IsNum(strBGH))
                {
                    modelFeeDZTruePayTab.intFiledReserv1 = Convert.ToInt32(CurrentUGR.Cells["制单人员"].Value.ToString(), 10);
                }
                else
                {

                    DataSet ds = m_bllT_ACL_User.GetList("Name = '" + strBGH + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        modelFeeDZTruePayTab.intFiledReserv1 = Convert.ToInt32(ds.Tables[0].Rows[0]["UserPasswordTabID"].ToString(), 10);
                    }
                }
                if (CurrentUGR.Cells["实付日期"].Text != "")
                {
                    DateTime dtTemp = Convert.ToDateTime(CurrentUGR.Cells["实付日期"].Value.ToString());
                    modelFeeDZTruePayTab.TruePayDate = dtTemp.Date;
                }

                if (modelFeeDZTruePayTab.ToPrivateBianHao == "" && modelFeeDZTruePayTab.ToPubBianHao == "")
                    modelFeeDZTruePayTab.InputDate = System.DateTime.Now.Date;

                modelFeeDZTruePayTab.TruePayMonth = Convert.ToInt32(CurrentUGR.Cells["实付费用计提年月"].Value.ToString(), 10);
                modelFeeDZTruePayTab.ShouldPayMonth = Convert.ToInt32(CurrentUGR.Cells["应收费用计提年月"].Value.ToString(), 10);
                modelFeeDZTruePayTab.bitFiledReserv1 = (bool)CurrentUGR.Cells["是否直接开票"].Value;


                modelFeeDZTruePayTab.JinBanPerson = modelFeeDZTruePayTab.JinBanPerson != ""
                    ? CurrentUGR.Cells["经办人"].Value.ToString()
                    : Frm_Main.m_strLoginUser;
                //modelFeeDZTruePayTab.RecvMonyePerson = CurrentUGR.Cells["收款人"].Value.ToString();
                modelFeeDZTruePayTab.RecvMonyePerson = modelFeeDZTruePayTab.RecvMonyePerson != ""
    ? CurrentUGR.Cells["收款人"].Value.ToString()
    : this.comboBoxRecvPerson.Text;
               // modelFeeDZTruePayTab.MakeSheetPerson = CurrentUGR.Cells["制单人员"].Value.ToString();

                //modelFeeDZTruePayTab.IsToRecvPayTab = Convert.ToInt32(CurrentUGR.Cells["是否已提取至应收应付登记表"].Value.ToString(), 10);
                //modelFeeDZTruePayTab.IsToFinCheckMoneyTab = Convert.ToInt32(CurrentUGR.Cells["是否已提取至财务对帐"].Value.ToString(), 10);
                modelFeeDZTruePayTab.Note = CurrentUGR.Cells["备注"].Value.ToString();
                if(CurrentUGR.Cells["送单日期"].Value.ToString() != "")
                    modelFeeDZTruePayTab.datetimeFiledReserv1 = Convert.ToDateTime(CurrentUGR.Cells["送单日期"].Value.ToString()); 
                //string strBiZhi = CurrentUGR.Cells["单证公司"].Value.ToString();
                //if (ClassInputValid.IsNum(strBiZhi))
                //{
                //    modelFeeDZTruePayTab.DZCompDataTabID = Convert.ToInt32(CurrentUGR.Cells["单证公司"].Value.ToString(), 10);
                //}
                //else
                //{
                //    DataSet ds = m_bllDZCompDataTab.GetList("DZCompName = '" + strBiZhi + "'");
                //    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                //    {
                //        modelFeeDZTruePayTab.DZCompDataTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["DZCompDataTabID"].ToString(), 10);
                //    }
                //}

                modelFeeDZTruePayTab.DZCompDataTabID = (int)this.comboBoxComp.SelectedValue;
                string strBiZhi = CurrentUGR.Cells["单证种类"].Value.ToString();
                if (ClassInputValid.IsNum(strBiZhi))
                {
                    modelFeeDZTruePayTab.DZCateTabID = Convert.ToInt32(CurrentUGR.Cells["单证种类"].Value.ToString(), 10);
                }
                else
                {
                    DataSet ds = m_bllDZCateTab.GetList("DZCateName = '" + strBiZhi + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        modelFeeDZTruePayTab.DZCateTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["DZCateTabID"].ToString(), 10);
                    }
                }
                //modelFeeDZTruePayTab.BiZhiTabID = Convert.ToInt32(CurrentUGR.Cells["币种"].Value.ToString(), 10);
                modelFeeDZTruePayTab.AddPageCounts = Convert.ToInt32(CurrentUGR.Cells["加页份数"].Value.ToString(), 10);
                modelFeeDZTruePayTab.DZCounts = Convert.ToInt32(CurrentUGR.Cells["单证份数"].Value.ToString(), 10);
                modelFeeDZTruePayTab.decimalFiledReserv3 = Convert.ToDecimal(CurrentUGR.Cells["其他费用金额"].Value.ToString());
               

                CurrentUGR.Cells["收款人"].Value = modelFeeDZTruePayTab.RecvMonyePerson;
                CurrentUGR.Cells["经办人"].Value = modelFeeDZTruePayTab.JinBanPerson;
                CurrentUGR.Cells["录入日期"].Value = modelFeeDZTruePayTab.InputDate;
                //CurrentUGR.Cells["单证项目金额"].Value
                //    = modelFeeDZTruePayTab.DZCounts * Convert.ToDecimal(CurrentUGR.Cells["单证价格"].Value.ToString());
                //CurrentUGR.Cells["加页金额"].Value
                //    = modelFeeDZTruePayTab.AddPageCounts * Convert.ToDecimal(CurrentUGR.Cells["加页价格"].Value.ToString());

                bRet = IsFeeTruePayLocked(CurrentUGR) && CheckFeeTruePayModify(modelFeeDZTruePayTab);
                if (bRet)
                {
                    MessageBox.Show("实付费用已锁定，不能修改或删除记录！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }

                m_bllFeeDZTruePayTab.Update(modelFeeDZTruePayTab);

                DataSet ds2 = m_bllFeeDZTruePayTab.GetList("FeeDZTruePayTabID = '" + nFeeDZTruePayTabID.ToString() + "'");
                DGBY.Model.FeeDZTruePayTab modelFeeDZTruePayTab2
                    = m_bllFeeDZTruePayTab.GetModel(nFeeDZTruePayTabID);

                Decimal dtModelShouldRecvMoneyOld = modelFeeDZTruePayTab2.decimalFiledReserv2;

                modelFeeDZTruePayTab2.decimalFiledReserv1 = (Decimal)ds2.Tables[0].Rows[0]["应付实付金额合计公式"];

                //20120512 修改应收金额合计规则
                if (nDZCateTabIDOld != modelFeeDZTruePayTab.DZCateTabID
                    ||nDZCountsOld != modelFeeDZTruePayTab.DZCounts
                    ||nAddPageCounts != modelFeeDZTruePayTab.AddPageCounts)
                {
                    modelFeeDZTruePayTab2.decimalFiledReserv2 = (Decimal)ds2.Tables[0].Rows[0]["应收金额合计公式"];
                }
                else
                {
                    modelFeeDZTruePayTab2.decimalFiledReserv2 = Convert.ToDecimal(CurrentUGR.Cells["应收金额合计"].Value.ToString());
                }
                
                m_bllFeeDZTruePayTab.Update(modelFeeDZTruePayTab2);


                

                /* 20121205 修改应收金额显示规则
                Decimal dtShouldRecvMoneyOld = (Decimal)CurrentUGR.Cells["应收金额合计"].Value;

                if (dtShouldRecvMoneyOld == 0)
                {
                     if (dtModelShouldRecvMoneyOld == 0)
                         CurrentUGR.Cells["应收金额合计"].Value = ds2.Tables[0].Rows[0]["应收金额合计公式"];
                }
                else
                {
                    if (dtShouldRecvMoneyOld == (Decimal)ds2.Tables[0].Rows[0]["应收金额合计公式"])
                    {
                        CurrentUGR.Cells["应收金额合计"].Value = ds2.Tables[0].Rows[0]["应收金额合计公式"];
                    }
          
                }

                modelFeeDZTruePayTab2.decimalFiledReserv2 = Convert.ToDecimal(CurrentUGR.Cells["应收金额合计"].Value.ToString());

                m_bllFeeDZTruePayTab.Update(modelFeeDZTruePayTab2);
                */

                /********************************************************/
                // 20121205 显示
                CurrentUGR.Cells["应收金额合计"].Value = modelFeeDZTruePayTab2.decimalFiledReserv2;
                /********************************************************/

                CurrentUGR.Cells["应付实付金额合计"].Value = ds2.Tables[0].Rows[0]["应付实付金额合计公式"];
                CurrentUGR.Cells["单证项目金额"].Value = ds2.Tables[0].Rows[0]["单证项目金额"];
                CurrentUGR.Cells["加页金额"].Value = ds2.Tables[0].Rows[0]["加页金额"];
                //CurrentUGR.Cells["应收金额合计"].Value = ds2.Tables[0].Rows[0]["应收金额合计公式"];
                CurrentUGR.Cells["单证公司"].Value = ds2.Tables[0].Rows[0]["单证公司"];
                //CurrentUGR.Cells["实付金额合计"].Value 
                //    = modelFeeDZTruePayTab.DZCounts * Convert.ToDecimal(CurrentUGR.Cells["单证价格"].Value.ToString())
                //    + modelFeeDZTruePayTab.AddPageCounts * Convert.ToDecimal(CurrentUGR.Cells["加页价格"].Value.ToString());

                DGBY.Model.PaidanRegisterTab modelPaidanRegisterTab = m_bllPaidanRegisterTab.GetModel(modelFeeDZTruePayTab2.PaidanRegisterTabID);
                if (null == modelPaidanRegisterTab)
                    Frm_Main.Log2DB(this.Text, "修改", modelFeeDZTruePayTab2.FeeDZTruePayTabID.ToString());
                else
                    Frm_Main.Log2DB(this.Text, "修改", ultraGrid1.ActiveRow.Cells["合同编号"].Value.ToString());

                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        private void ultraGrid1_BeforeRowUpdate(object sender, CancelableRowEventArgs e)
        {
            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[e.Row.Index];

                if (m_bInsertFlag)
                {
                    ultraGrid1.Rows[e.Row.Index].Cells[2].Activation = Activation.ActivateOnly;
                    m_bInsertFlag = false;

                }
                else//修改
                {

                    ModifyGoodsDetail(e.Row.Index);

                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }

        private bool AddGoodsDetail(int nRowIndex)
        {

            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nRowIndex];

                DGBY.Model.FeeDZTruePayTab modelFeeDZTruePayTab = new DGBY.Model.FeeDZTruePayTab();
                modelFeeDZTruePayTab = m_modelFeeDZTruePayTab;
                modelFeeDZTruePayTab.decimalFiledReserv2 = 0;
                modelFeeDZTruePayTab.ToPrivateBianHao = "";
                modelFeeDZTruePayTab.ToPubBianHao = "";
                modelFeeDZTruePayTab.TruePayDate = null;
                m_bllFeeDZTruePayTab.Add(modelFeeDZTruePayTab);

                DGBY.Model.PaidanRegisterTab modelPaidanRegisterTab = m_bllPaidanRegisterTab.GetModel(modelFeeDZTruePayTab.PaidanRegisterTabID);
                if (modelPaidanRegisterTab != null)
                    Frm_Main.Log2DB(this.Text, "复制增加", modelPaidanRegisterTab.ContractCode);
                else
                    Frm_Main.Log2DB(this.Text, "复制增加", modelFeeDZTruePayTab.FeeDZTruePayTabID.ToString());
                ShowAllRecord("FeeDZTruePayTab.PaidanRegisterTabID = '" + modelFeeDZTruePayTab.PaidanRegisterTabID + "'");


                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        private void InitNewAddRowValueCopyContract(int nIndex)
        {

            UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];

            CurrentUGR.Cells["合同编号"].Value = m_modelFeeDZTruePayTab.PaidanRegisterTabID;
            CurrentUGR.Cells["对公费用编号"].Value = m_modelFeeDZTruePayTab.ToPubBianHao;
            CurrentUGR.Cells["对私费用编号"].Value = m_modelFeeDZTruePayTab.ToPrivateBianHao;
            if (m_modelFeeDZTruePayTab.TruePayDate != null)
                CurrentUGR.Cells["实付日期"].Value = m_modelFeeDZTruePayTab.TruePayDate;
            if (m_modelFeeDZTruePayTab.InputDate != null)
                CurrentUGR.Cells["录入日期"].Value = m_modelFeeDZTruePayTab.InputDate;

            CurrentUGR.Cells["实付费用计提年月"].Value = m_modelFeeDZTruePayTab.TruePayMonth;
            CurrentUGR.Cells["应收费用计提年月"].Value = m_modelFeeDZTruePayTab.ShouldPayMonth;
            //CurrentUGR.Cells["实付金额"].Value = m_modelFeeDZTruePayTab.TruePayMoney;

            //CurrentUGR.Cells["对公公式"].Value = m_modelFeeDZTruePayTab.ToPubFormula;
            //CurrentUGR.Cells["对私公式"].Value = m_modelFeeDZTruePayTab.ToPrivateFormula;
            CurrentUGR.Cells["经办人"].Value = m_modelFeeDZTruePayTab.JinBanPerson;
            CurrentUGR.Cells["收款人"].Value = m_modelFeeDZTruePayTab.RecvMonyePerson;

            //CurrentUGR.Cells["是否已提取至应收应付登记表"].Value = m_modelFeeDZTruePayTab.IsToRecvPayTab;
            //CurrentUGR.Cells["是否已提取至财务对帐"].Value = m_modelFeeDZTruePayTab.IsToFinCheckMoneyTab;

      
            CurrentUGR.Cells["制单人员"].Value = m_modelFeeDZTruePayTab.intFiledReserv1;
            CurrentUGR.Cells["送单日期"].Value = m_modelFeeDZTruePayTab.datetimeFiledReserv1;
         

            if (m_bInsertFlag)
            {
                ultraGrid1.Rows[nIndex].Cells[2].Activation = Activation.ActivateOnly;
                m_bInsertFlag = false;
                AddGoodsDetail(nIndex);

            }

        }

        private void ultraGrid1_AfterCellActivate(object sender, EventArgs e)
        {
            int iIndex = this.ultraGrid1.ActiveCell.Row.Index;
            if (iIndex < 0)
                return;
            m_nDelIndex = ultraGrid1.ActiveRow.Index;

        }
         
          
        private bool AddGoodsDetailNoContractNo(int nRowIndex)
        {

            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nRowIndex];

                DGBY.Model.FeeDZTruePayTab modelFeeDZTruePayTab = new DGBY.Model.FeeDZTruePayTab();

                modelFeeDZTruePayTab.ToPrivateBianHao = "";
                modelFeeDZTruePayTab.ToPubBianHao = "";
                modelFeeDZTruePayTab.TruePayDate = null;

                modelFeeDZTruePayTab.DZCounts = 0;
                modelFeeDZTruePayTab.AddPageCounts = 0;
                modelFeeDZTruePayTab.FactoryDataTabID = m_nFacID;
                modelFeeDZTruePayTab.PaidanRegisterTabID = -1;

                modelFeeDZTruePayTab.DZCompDataTabID = 1;

                modelFeeDZTruePayTab.DZCateTabID = 1;
                modelFeeDZTruePayTab.TruePayDate = null;

                DateTime dtNow = System.DateTime.Now.Date;
                modelFeeDZTruePayTab.SBDate = dtNow;
                modelFeeDZTruePayTab.InputDate = dtNow;
                modelFeeDZTruePayTab.TruePayMonth = 1;
                modelFeeDZTruePayTab.ShouldPayMonth = 1;

                //modelFeeDZTruePayTab.ToPubFormula = "";
                //modelFeeDZTruePayTab.ToPrivateFormula = "";


                modelFeeDZTruePayTab.JinBanPerson = "";
                modelFeeDZTruePayTab.RecvMonyePerson = "";
                modelFeeDZTruePayTab.MakeSheetPerson = "";


                //modelFeeDZTruePayTab.IsToRecvPayTab = Convert.ToInt32(CurrentUGR.Cells["是否已提取至应收应付登记表"].Value.ToString(), 10);
                //modelFeeDZTruePayTab.IsToFinCheckMoneyTab = Convert.ToInt32(CurrentUGR.Cells["是否已提取至财务对帐"].Value.ToString(), 10);
                modelFeeDZTruePayTab.Note = "";



                modelFeeDZTruePayTab.bitFiledReserv1 = false;
                modelFeeDZTruePayTab.bitFiledReserv2 = false;

                modelFeeDZTruePayTab.stringFiledReserv1 = "";
                modelFeeDZTruePayTab.stringFiledReserv2 = "";
                modelFeeDZTruePayTab.stringFiledReserv3 = "";
                modelFeeDZTruePayTab.stringFiledReserv4 = "";

                modelFeeDZTruePayTab.intFiledReserv1 = 0;
                modelFeeDZTruePayTab.intFiledReserv2 = 0;
                modelFeeDZTruePayTab.intFiledReserv3 = 0;
                modelFeeDZTruePayTab.intFiledReserv4 = 0;
                modelFeeDZTruePayTab.datetimeFiledReserv1 = dtNow;
                modelFeeDZTruePayTab.datetimeFiledReserv2 = dtNow;

                modelFeeDZTruePayTab.decimalFiledReserv1 = 0;
                modelFeeDZTruePayTab.decimalFiledReserv2 = 0;
                modelFeeDZTruePayTab.decimalFiledReserv3 = 0;
                modelFeeDZTruePayTab.decimalFiledReserv4 = 0;


                int nID = m_bllFeeDZTruePayTab.Add(modelFeeDZTruePayTab);
                Frm_Main.Log2DB(this.Text, "增加无合同号记录", nID.ToString());
                ShowAllRecord("FeeDZTruePayTab.FeeDZTruePayTabID = '" + nID + "'");
                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }


        private void InitNewAddRowValueNoContract(int nIndex)
        {

            UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];

            CurrentUGR.Cells["合同编号"].Value = "";
            CurrentUGR.Cells["对公费用编号"].Value = m_modelFeeDZTruePayTab.ToPubBianHao;
            CurrentUGR.Cells["对私费用编号"].Value = m_modelFeeDZTruePayTab.ToPrivateBianHao;
            if (m_modelFeeDZTruePayTab.TruePayDate != null)
                CurrentUGR.Cells["实付日期"].Value = m_modelFeeDZTruePayTab.TruePayDate;
            if (m_modelFeeDZTruePayTab.InputDate != null)
                CurrentUGR.Cells["录入日期"].Value = m_modelFeeDZTruePayTab.InputDate;

            CurrentUGR.Cells["实付费用计提年月"].Value = m_modelFeeDZTruePayTab.TruePayMonth;
            CurrentUGR.Cells["应收费用计提年月"].Value = m_modelFeeDZTruePayTab.ShouldPayMonth;

            CurrentUGR.Cells["经办人"].Value = m_modelFeeDZTruePayTab.JinBanPerson;
            CurrentUGR.Cells["收款人"].Value = m_modelFeeDZTruePayTab.RecvMonyePerson;

            CurrentUGR.Cells["备注"].Value = m_modelFeeDZTruePayTab.Note;
            CurrentUGR.Cells["制单人员"].Value = m_modelFeeDZTruePayTab.intFiledReserv1;
            CurrentUGR.Cells["送单日期"].Value = m_modelFeeDZTruePayTab.datetimeFiledReserv1;
          
            if (m_bInsertFlag)
            {
                ultraGrid1.Rows[nIndex].Cells[2].Activation = Activation.ActivateOnly;
                m_bInsertFlag = false;
                AddGoodsDetailNoContractNo(nIndex);

            }

        }
        private void ultraGrid1_AfterRowInsert(object sender, RowEventArgs e)
        {
            ultraGrid1.Rows[e.Row.Index].Cells[1].IgnoreRowColActivation = true;
            ultraGrid1.Rows[e.Row.Index].Cells[1].Activation = Activation.AllowEdit;
            ultraGrid1.Rows[e.Row.Index].Cells[1].Activate();
            ultraGrid1.PerformAction(UltraGridAction.EnterEditMode);
            m_bInsertFlag = true;
            if (m_InsertType == InsertType.Common)
                InitNewAddRowValueCopyContract(e.Row.Index);
            else if (m_InsertType == InsertType.WithoutContractNo)
                InitNewAddRowValueNoContract(e.Row.Index);
        }

        private void ultraGrid1_BeforeRowsDeleted(object sender, BeforeRowsDeletedEventArgs e)
        {
            e.DisplayPromptMsg = false;
            if (!m_bInsertFlag)
            {
                int nSelectFeeDZTruePayTabID = Convert.ToInt32(ultraGrid1.ActiveRow.Cells["FeeDZTruePayTabID"].Value.ToString(), 10);
                DGBY.Model.FeeDZTruePayTab modelFeeDZTruePayTabOld
                    = m_bllFeeDZTruePayTab.GetModel(nSelectFeeDZTruePayTabID);

                if (modelFeeDZTruePayTabOld == null)
                {
                    MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                if (MessageBox.Show("确实要删除该条记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    try
                    {

                        m_bllFeeDZTruePayTab.Delete(nSelectFeeDZTruePayTabID);

                        DGBY.Model.PaidanRegisterTab modelPaidanRegisterTab = m_bllPaidanRegisterTab.GetModel(modelFeeDZTruePayTabOld.PaidanRegisterTabID);
                        if (null == modelPaidanRegisterTab)
                            Frm_Main.Log2DB(this.Text, "删除", modelFeeDZTruePayTabOld.FeeDZTruePayTabID.ToString());
                        else
                            Frm_Main.Log2DB(this.Text, "删除", ultraGrid1.ActiveRow.Cells["合同编号"].Value.ToString());

                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        e.Cancel = true;
                    }
                }
                else
                {
                    e.Cancel = true;
                }
            }
            else
            {
                MessageBox.Show("插入未完成，不能删除记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Cancel = true;
            }
        }

        private void 复制增加ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (!m_bInsertFlag)
                {
                    UltraGridRow CurrentUGRFrom;
                    CurrentUGRFrom = ultraGrid1.Rows[m_nDelIndex];
                    int nSelectFeeDZTruePayTabID = Convert.ToInt32(CurrentUGRFrom.Cells[0].Value.ToString(), 10);
                    m_modelFeeDZTruePayTab = m_bllFeeDZTruePayTab.GetModel(nSelectFeeDZTruePayTabID);
                    m_modelFeeDZTruePayTab.InputDate = System.DateTime.Now.Date;
                    // m_bCopyInsertFlag = true;
                    m_InsertType = InsertType.Common;
                    UltraGridRow row = this.ultraGrid1.DisplayLayout.Bands[0].AddNew();

                }
            }
            catch
            {
                MessageBox.Show("没有选择记录，无法复制！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private bool CheckModify(UltraGridRow CurrentUGR)
        {
            int nYearMonth = Convert.ToInt32(CurrentUGR.Cells["应收费用计提年月"].Value.ToString(), 10);
            DataSet ds = m_bllDG_FeeLockTab.GetList("FeeName = '单证' and IsLock = 1 and LockYearMonth = '" + nYearMonth.ToString() + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                return false;
            }
            return true;
        }
        private bool IsFeeTruePayLocked(UltraGridRow CurrentUGR)
        {
            int nYearMonth = Convert.ToInt32(CurrentUGR.Cells["实付费用计提年月"].Value.ToString(), 10);
            DataSet ds = m_bllDG_FeeTruePayLockTab.GetList("FeeName = '单证' and IsLock = 1 and LockYearMonth = '" + nYearMonth.ToString() + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
        private bool CheckFeeTruePayModify(DGBY.Model.FeeDZTruePayTab modelFeeDZTruePayTab)
        {
            DGBY.Model.FeeDZTruePayTab oldModelFeeDZTruePayTab = m_bllFeeDZTruePayTab.GetModel(modelFeeDZTruePayTab.FeeDZTruePayTabID);
            if (null != oldModelFeeDZTruePayTab)
            {
                //限制修改实付日期、费用编号、是否直接开票、应付实付金额
                if (oldModelFeeDZTruePayTab.TruePayDate != modelFeeDZTruePayTab.TruePayDate
                    || oldModelFeeDZTruePayTab.bitFiledReserv1 != modelFeeDZTruePayTab.bitFiledReserv1
                    || oldModelFeeDZTruePayTab.decimalFiledReserv1 != modelFeeDZTruePayTab.decimalFiledReserv1
                    || oldModelFeeDZTruePayTab.ToPubBianHao != modelFeeDZTruePayTab.ToPubBianHao
                    || oldModelFeeDZTruePayTab.ToPrivateBianHao != modelFeeDZTruePayTab.ToPrivateBianHao)
                    return true;
            }

            return false;
        }
        private void 删除本行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确实要删除该记录么!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            int nIndex = m_nDelIndex;
            if (m_nDelIndex >= 0)
            {
                if (ultraGrid1.ActiveRow != null)
                {
                    UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];

                    bool bRet = CheckModify(CurrentUGR);
                    if (!bRet)
                    {
                        MessageBox.Show("费用已锁定，不能修改或删除记录！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }

                    bRet = IsFeeTruePayLocked(CurrentUGR);
                    if (bRet)
                    {
                        MessageBox.Show("实付费用已锁定，不能修改或删除记录！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }

                    int nPaidanRegisterTabID = Convert.ToInt32(CurrentUGR.Cells["PaidanRegisterTabID"].Value.ToString(), 10);
                    DataSet ds = m_bllFeeDZTruePayTab.GetList("FeeDZTruePayTab.PaidanRegisterTabID = '" + nPaidanRegisterTabID.ToString() + "'");
                    if (ds.Tables[0].Rows.Count > 1)
                        ultraGrid1.ActiveRow.Delete(false);
                    else
                    {
                        MessageBox.Show("系统中该合同号只剩下一条记录，为系统自动生成，不能删除!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                }
            }
            else
            {
                MessageBox.Show("请选中要删除的记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private string MakeDZBianHao(string strDepart, string strComp, string strPuborPri, string strDate)
        {

            string strRet = "DZ" + ClassUtility.m_dicCn2EngDep[strDepart]
                + ClassUtility.m_dicCn2EngComp[strComp] + ClassUtility.m_dicCn2EngGS[strPuborPri] + strDate;

            if (m_strBianHaoTemp != "")
            {
                strRet += m_strBianHaoTemp;
            }
            else
            {
                DateTime dtTruePay = this.dateTimePicker1.Value.Date;
                DataSet dsTemp;
                //if (strPuborPri == "公")
                //    dsTemp = m_bllFeeSJTruePayTab.GetMaxList("TruePayDate = '" + dtTruePay + @"' and ToPubBianHao like '%" + strRet + "%'");
                //else

                dsTemp = m_bllFeeDZTruePayTab.GetMaxList("TruePayDate = '" + dtTruePay + "'");


                if (dsTemp.Tables[0].Rows.Count > 0)
                {
                    string strTest = "";

                    string strTestPub = dsTemp.Tables[0].Rows[0]["对公费用编号"].ToString();

                    string strTestPri = dsTemp.Tables[0].Rows[0]["对私费用编号"].ToString();

                    if (strTestPri.CompareTo(strTestPub) >= 0)
                        strTest = strTestPri;
                    else
                        strTest = strTestPub;


                    string strTemp = strTest.Remove(0, 8);
                    if (strTemp != "")
                    {
                        int nTemp = Convert.ToInt32(strTemp, 10) + 1;
                        strTemp = nTemp.ToString().PadLeft(3, '0'); ;
                    }
                    else
                    {
                        strTemp = "001";
                    }

                    strRet += strTemp;
                    m_strBianHaoTemp = strTemp;
                }
                else
                {
                    strRet = strRet + "001";
                    m_strBianHaoTemp = "001";
                }
            }

            return strRet;
        }

        private string MakeDZBianHaoWithoutLiuShuiNo(string strDepart, string strComp
 , string strPuborPri, string strDate)
        {
            string strRet = "DZ" + ClassUtility.m_dicCn2EngDep[strDepart]
+ ClassUtility.m_dicCn2EngComp[strComp] + ClassUtility.m_dicCn2EngGS[strPuborPri] + strDate;

            //string strRet = "DZ" + strDepart + strComp + strPuborPri + strDate;
            return strRet;
        }

        private void buttonBatchModifyDate_Click(object sender, EventArgs e)
        {
            m_strBianHaoTemp = "";
            m_SQLStringList.Clear();
            m_arrayList.Clear();

            try
            {
                Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();
                DialogResult dr = MessageBox.Show("确实要批量修改以下" + ugr.Length.ToString() + "条记录的实付日期么", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (dr == DialogResult.Cancel)
                    return;
                bool bPub = false;
                bool bPri = false;
                for (int i = 0; i < ugr.Length; i++)
                {
                    int nID = Convert.ToInt32(ugr[i].Cells[0].Value.ToString(), 10);
                    DGBY.Model.FeeDZTruePayTab modelFeeDZTruePayTabOld
                                        = m_bllFeeDZTruePayTab.GetModel(nID);

                    DateTime? dtTruePayOld = modelFeeDZTruePayTabOld.TruePayDate;

                    modelFeeDZTruePayTabOld.TruePayDate = this.dateTimePicker1.Value.Date;

                    UltraGridRow CurrentUGR = ultraGrid1.Rows[ugr[i].Index];

                    if (dtTruePayOld == null && modelFeeDZTruePayTabOld.RecvMonyePerson != "")
                    {
                        bPri = true;
                        string strBianHao = MakeDZBianHao(ugr[i].Cells["部门"].Value.ToString()
                       , ugr[i].Cells["经营单位"].Value.ToString()
                       , "私"
                        , this.dateTimePicker1.Value.Date.ToString("yyyyMMdd"));

                        string strIsFacDirect = (bool)ugr[i].Cells["是否直接开票"].Value ? "G" : "B";
                        string strRet = strBianHao.Insert(7, strIsFacDirect);

                        modelFeeDZTruePayTabOld.ToPrivateBianHao = strRet;
                        CurrentUGR.Cells["对私费用编号"].Value = strRet;


                    }
                    if (dtTruePayOld == null && modelFeeDZTruePayTabOld.RecvMonyePerson == "")
                    {
                        bPub = true;
                        string strBianHao = MakeDZBianHao(ugr[i].Cells["部门"].Value.ToString()
                       , ugr[i].Cells["经营单位"].Value.ToString()
                       , "公"
                        , this.dateTimePicker1.Value.Date.ToString("yyyyMMdd"));

                        string strIsFacDirect = (bool)ugr[i].Cells["是否直接开票"].Value ? "G" : "B";
                        string strRet = strBianHao.Insert(7, strIsFacDirect);

                        modelFeeDZTruePayTabOld.ToPubBianHao = strRet;
                        CurrentUGR.Cells["对公费用编号"].Value = strRet;

                    }
                    
                    modelFeeDZTruePayTabOld.bitFiledReserv2 = false;//编号是否手动生成

                    CurrentUGR.Cells["实付日期"].Value = this.dateTimePicker1.Value.Date;
                    CurrentUGR.Cells["编号是否手动生成"].Value = false;
                    m_bllFeeDZTruePayTab.TransUpdate(modelFeeDZTruePayTabOld
                    , m_SQLStringList, m_arrayList, i);
                }

                if (bPri && bPub)
                {
                    MessageBox.Show("不能同时生成对公对私编号!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    m_bllSJPackingTab.ExecuteSqlTran(m_SQLStringList, m_arrayList);
                    Frm_Main.Log2DB(this.Text, "批量修改实付日期", this.dateTimePicker1.Value.Date.ToString());
                    MessageBox.Show("批量修改实付日期完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

        }

        private void buttonExportExcel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ultraGrid1.DataSource;

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    ultraGridExcelExporter1.Export(ultraGrid1, outFile);
                }
            }
        }

        private void buttonSetEmpty_Click(object sender, EventArgs e)
        {
       
            m_SQLStringList.Clear();
            m_arrayList.Clear();

            try
            {
                bool bCanModify = true;

                Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();

               
                DialogResult dr = MessageBox.Show("确实要批量置空以下" + ugr.Length.ToString() + "条记录的实付日期么", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (dr == DialogResult.Cancel)
                    return;

                List<string> lstDT = new List<string>();
                for (int i = 0; i < ugr.Length; i++)
                {
                    string strTemp = ugr[i].Cells["对公费用编号"].Value.ToString() + ugr[i].Cells["对私费用编号"].Value.ToString();

                    if (strTemp != "" && lstDT.Count == 0)
                        lstDT.Add(strTemp);
                    else if (lstDT.Count > 0 && !lstDT.Contains(strTemp))
                    {
                        bCanModify = false;
                        break;
                    }

                }

                if (!bCanModify)
                {
                    MessageBox.Show("费用编号不相同，不能批量置空实付日期!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();
                for (int i = 0; i < ugr.Length; i++)
                {
                    UltraGridRow CurrentUGR = ultraGrid1.Rows[ugr[i].Index];
                    int nID = Convert.ToInt32(ugr[i].Cells[0].Value.ToString(), 10);
                    DGBY.Model.FeeDZTruePayTab modelFeeDZTruePayTabOld
                                        = m_bllFeeDZTruePayTab.GetModel(nID);
                    modelFeeDZTruePayTabOld.TruePayDate = null;
                    modelFeeDZTruePayTabOld.ToPrivateBianHao = "";
                    modelFeeDZTruePayTabOld.ToPubBianHao = "";

                    m_bllFeeDZTruePayTab.TransUpdate(modelFeeDZTruePayTabOld
                        , m_SQLStringList, m_arrayList, i);
                    CurrentUGR.Cells["对私费用编号"].Value = "";
                    CurrentUGR.Cells["对公费用编号"].Value = "";
                    CurrentUGR.Cells["实付日期"].Value = DBNull.Value;
                }

                m_bllSJPackingTab.ExecuteSqlTran(m_SQLStringList, m_arrayList);
                Frm_Main.Log2DB(this.Text, "批量置空实付日期", "");
                MessageBox.Show("批量置空实付日期完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }

        private void Frm_FeeDZTruePayTab_Load_1(object sender, EventArgs e)
        {
            InitComBox();
            ClassUltraGridCommonSet.InitUltraWinGridSetting(this.ultraGrid1);
        
        }

        private void 增加无合同号记录ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (m_nDelIndex < 0)
                return;
            if (!m_bInsertFlag)
            {
                m_InsertType = InsertType.WithoutContractNo;
                DataSet ds;
                UltraGridRow CurrentUGR = ultraGrid1.Rows[m_nDelIndex];
                string strFac = CurrentUGR.Cells["工厂简称"].Value.ToString();
                if (ClassInputValid.IsNum(strFac))
                {
                    m_nFacID = Convert.ToInt32(CurrentUGR.Cells["工厂简称"].Value.ToString(), 10);
                }
                else
                {
                    ds = m_bllFactoryDataTab.GetList("FactoryNameJX = '" + strFac + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        m_nFacID = Convert.ToInt32(ds.Tables[0].Rows[0]["FactoryDataTabID"].ToString(), 10);
                    }
                }
                UltraGridRow row = this.ultraGrid1.DisplayLayout.Bands[0].AddNew();
                //  row.Cells[2] = DateTime.Parse("1900-01-01");
            }
        }

        private void comboBoxComp_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string str = this.comboBoxComp.SelectedValue.ToString();
                int nID = Convert.ToInt32(str, 10);
                DataSet ds = m_bllDZRecvPeoTelTab.GetList("DZCompDataTabID = '" + nID + "'");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    this.comboBoxRecvPerson.DataSource = ds.Tables[0];

                    this.comboBoxRecvPerson.ValueMember = "DZRecvPeoTelTabID";
                    this.comboBoxRecvPerson.DisplayMember = "单证公司收款人";
                    this.comboBoxRecvPerson.AutoCompleteSource = AutoCompleteSource.ListItems;
                    this.comboBoxRecvPerson.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                    this.comboBoxRecvPerson.SelectedIndex = 0;
                }
                else
                {
                    this.comboBoxRecvPerson.DataSource = null;
                }



            }
            catch { }
        }

        private void buttonModYearMon_Click(object sender, EventArgs e)
        {
            if (!ClassInputValid.IsNum(textBoxYearMonth.Text))
            {
                errorProvider1.SetError(textBoxYearMonth, "必须输入数字");
                return;
            }
            int nYearMon = Convert.ToInt32(textBoxYearMonth.Text, 10);
            if (nYearMon < 201001 || nYearMon > 219912)
            {
                errorProvider1.SetError(textBoxYearMonth, "必须正确的年月，形如201206");
                return;
            }

            m_SQLStringList.Clear();
            m_arrayList.Clear();

            try
            {
                Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();

                DialogResult dr = MessageBox.Show("确实要批量修改以下" + ugr.Length.ToString() + "条记录的应收费用计提年月么", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (dr == DialogResult.Cancel)
                    return;


                for (int i = 0; i < ugr.Length; i++)
                {

                    int nID = Convert.ToInt32(ugr[i].Cells[0].Value.ToString(), 10);
                    DGBY.Model.FeeDZTruePayTab modelFeeDZTruePayTabOld
                                        = m_bllFeeDZTruePayTab.GetModel(nID);
                    modelFeeDZTruePayTabOld.ShouldPayMonth = nYearMon;
                    UltraGridRow CurrentUGR = ultraGrid1.Rows[ugr[i].Index];

                    m_bllFeeDZTruePayTab.TransUpdate(modelFeeDZTruePayTabOld
                        , m_SQLStringList, m_arrayList, i);

                    CurrentUGR.Cells["应收费用计提年月"].Value = nYearMon.ToString();
                }

                m_bllSJPackingTab.ExecuteSqlTran(m_SQLStringList, m_arrayList);
                Frm_Main.Log2DB(this.Text, "批量修改计提年月", nYearMon.ToString());
                MessageBox.Show("批量修改应收费用计提年月完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }

        private void 清空该条记录ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确实要清空该记录么!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            int nIndex = m_nDelIndex;
            if (m_nDelIndex >= 0)
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nIndex];
                bool bRet = CheckModify(CurrentUGR);
                if (!bRet)
                {
                    MessageBox.Show("费用已锁定，不能修改或删除记录！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                bRet = IsFeeTruePayLocked(CurrentUGR);
                if (bRet)
                {
                    MessageBox.Show("实付费用已锁定，不能修改或删除记录！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                ResetUltraGridRow(nIndex);

                int nFeeSJTruePayTabID = Convert.ToInt32(CurrentUGR.Cells[0].Value.ToString(), 10);
                ResetRecord(nFeeSJTruePayTabID);
                m_bClearing = true;
            }
            else
            {
                MessageBox.Show("请选中要清空的记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            m_SQLStringList.Clear();
            m_arrayList.Clear();

            try
            {
                Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();

                DialogResult dr = MessageBox.Show("确实要批量置空以下" + ugr.Length.ToString() + "条记录的应收费用计提年月么", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (dr == DialogResult.Cancel)
                    return;


                for (int i = 0; i < ugr.Length; i++)
                {

                    int nID = Convert.ToInt32(ugr[i].Cells[0].Value.ToString(), 10);
                    DGBY.Model.FeeDZTruePayTab modelFeeDZTruePayTabOld
                                        = m_bllFeeDZTruePayTab.GetModel(nID);
                    modelFeeDZTruePayTabOld.ShouldPayMonth = 0;
                    UltraGridRow CurrentUGR = ultraGrid1.Rows[ugr[i].Index];

                    m_bllFeeDZTruePayTab.TransUpdate(modelFeeDZTruePayTabOld
                        , m_SQLStringList, m_arrayList, i);

                    CurrentUGR.Cells["应收费用计提年月"].Value = "0";
                }

                m_bllSJPackingTab.ExecuteSqlTran(m_SQLStringList, m_arrayList);
                Frm_Main.Log2DB(this.Text, "批量置空计提年月", "");
                MessageBox.Show("批量置空应收费用计提年月完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }
        private bool IsLiuShuiNo(string strInput)
        {
            return Regex.IsMatch(strInput, @"^[0-9]{3}$");
        }
        private bool IsLiuShuiNo(ErrorProvider errorProvider1, TextBox TextBox)
        {
            if (TextBox.Text == "")
            {
                errorProvider1.SetError(TextBox, "内容不能为空！");
                return false;
            }
            else if (!IsLiuShuiNo(TextBox.Text))
            {
                errorProvider1.SetError(TextBox, "必须001-999之间的流水号");
                return false;
            }
            else
            {
                errorProvider1.SetError(TextBox, "");
                return true;
            }
        }
        private void buttonBatchModFeeNo_Click(object sender, EventArgs e)
        {
            m_SQLStringList.Clear();
            m_arrayList.Clear();

            try
            {
                if (!IsLiuShuiNo(this.errorProvider1, this.textBoxLiuShuiNo))
                    return;
                Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();

                DialogResult dr = MessageBox.Show("确实要批量修改以下" + ugr.Length.ToString() + "条记录的费用编号么", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (dr == DialogResult.Cancel)
                    return;

                for (int i = 0; i < ugr.Length; i++)
                {

                    int nID = Convert.ToInt32(ugr[i].Cells[0].Value.ToString(), 10);
                    DGBY.Model.FeeDZTruePayTab modelFeeDZTruePayTabOld
                                        = m_bllFeeDZTruePayTab.GetModel(nID);

                    DateTime? dtTruePayOld = modelFeeDZTruePayTabOld.TruePayDate;
                    if (dtTruePayOld != null)
                    {
                        MessageBox.Show("原记录实付日期不为空，不能修改，若要修改，请先置空实付日期!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }

                    modelFeeDZTruePayTabOld.TruePayDate = this.dateTimePicker1.Value.Date;

                    UltraGridRow CurrentUGR = ultraGrid1.Rows[ugr[i].Index];

                   // if (modelFeeDZTruePayTabOld.ToPrivateFormula != "" || modelFeeDZTruePayTabOld.ToPubFormula != "")
                    {
                        if (modelFeeDZTruePayTabOld.RecvMonyePerson != "")
                        {
                            string strBianHao = MakeDZBianHaoWithoutLiuShuiNo(ugr[0].Cells["部门"].Value.ToString()
                                , ugr[i].Cells["经营单位"].Value.ToString()
                  , "私"
                   , this.dateTimePicker1.Value.Date.ToString("yyyyMMdd")
                   );

                            string strIsFacDirect = (bool)ugr[i].Cells["是否直接开票"].Value ? "G" : "B";
                            string strRet = strBianHao.Insert(7, strIsFacDirect) + this.textBoxLiuShuiNo.Text;

                            modelFeeDZTruePayTabOld.ToPrivateBianHao = strRet;
                            CurrentUGR.Cells["对私费用编号"].Value = strRet;
                        }

                        if (modelFeeDZTruePayTabOld.RecvMonyePerson == "")
                        {
                            string strBianHao = MakeDZBianHaoWithoutLiuShuiNo(ugr[0].Cells["部门"].Value.ToString()
                                , ugr[i].Cells["经营单位"].Value.ToString()
                   , "公"
                   , this.dateTimePicker1.Value.Date.ToString("yyyyMMdd")
                   );

                            string strIsFacDirect = (bool)ugr[i].Cells["是否直接开票"].Value ? "G" : "B";
                            string strRet = strBianHao.Insert(7, strIsFacDirect) + this.textBoxLiuShuiNo.Text;

                            modelFeeDZTruePayTabOld.ToPubBianHao = strRet;
                            CurrentUGR.Cells["对公费用编号"].Value = strRet;
                        }

                        modelFeeDZTruePayTabOld.bitFiledReserv2 = true;//编号是否手动生成
                        m_bllFeeDZTruePayTab.TransUpdate(modelFeeDZTruePayTabOld
                            , m_SQLStringList, m_arrayList, i);

                        CurrentUGR.Cells["实付日期"].Value = this.dateTimePicker1.Value.Date;
                        CurrentUGR.Cells["编号是否手动生成"].Value = true;
                    }


                }

                m_bllSJPackingTab.ExecuteSqlTran(m_SQLStringList, m_arrayList);
                Frm_Main.Log2DB(this.Text, "批量修改费用编号", this.textBoxLiuShuiNo.Text);
                MessageBox.Show("批量修改费用编号完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }

        private void buttonModRecvPerson_Click(object sender, EventArgs e)
        {
            m_SQLStringList.Clear();
            m_arrayList.Clear();

            try
            {

                Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();

                DialogResult dr = MessageBox.Show("确实要批量修改以下" + ugr.Length.ToString() + "条记录的收款人么", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (dr == DialogResult.Cancel)
                    return;

                for (int i = 0; i < ugr.Length; i++)
                {

                    int nID = Convert.ToInt32(ugr[i].Cells[0].Value.ToString(), 10);
                    DGBY.Model.FeeDZTruePayTab modelFeeDZTruePayTabOld
                                        = m_bllFeeDZTruePayTab.GetModel(nID);

   

            

                    UltraGridRow CurrentUGR = ultraGrid1.Rows[ugr[i].Index];


                    CurrentUGR.Cells["收款人"].Value = this.textBoxRecvPerson.Text; 

                    modelFeeDZTruePayTabOld.RecvMonyePerson = this.textBoxRecvPerson.Text;//编号是否手动生成
                    m_bllFeeDZTruePayTab.TransUpdate(modelFeeDZTruePayTabOld
                        , m_SQLStringList, m_arrayList, i);

        


                }

                m_bllSJPackingTab.ExecuteSqlTran(m_SQLStringList, m_arrayList);
                Frm_Main.Log2DB(this.Text, "批量修改收款人", this.textBoxRecvPerson.Text);
                MessageBox.Show("批量修改收款人完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }




    }
}
